package com.inspur.attendance.Controller;

import com.inspur.attendance.Dao.DepartmentDao;
import com.inspur.attendance.Dao.UserDao;
import com.inspur.attendance.Entity.User;
import com.inspur.attendance.Service.UserService;
import com.inspur.attendance.Tools.Operation;
import com.inspur.attendance.Tools.Return;
import com.inspur.attendance.Tools.TimeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class UserController {
    @Autowired
    UserDao userDao;
    @Autowired
    UserService userService;
    @Autowired
    DepartmentDao departmentDao;

    /**
     * 获取所有用户信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllUser")
    public Return findAllUser(@Param("pageNo") Integer pageNo,
                              @Param("pageSize") Integer pageSize) {
        Page<User> page1 = null;
        try {
            page1 = userService.getPage(pageNo, pageSize);
            return Return.success(page1);
        } catch (Exception e) {
            return Return.failure();
        }
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delUser")
    @Operation
    public Return delUser(@Param("id") Long id) throws Exception {
        if (userService.delUser(id)) {
            return Return.success("删除用户"+id+"成功");
        } else {
            return Return.failure();
        }
    }

    // 模糊查询用户的真实姓名
    @RequestMapping("/findUserByName")
    public Return findUserByName(@Param("name") String name) {
        try {
            return Return.success(userService.findUserByName(name));
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

    @RequestMapping("/findUserByAccount")
    public Return findUserByAccount(@Param("account") String account) {
        System.out.println(account);
        try {
            return Return.success(userService.findByAccount(account));
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

    @RequestMapping("/editUser")
    @Operation
    public Return editUser(@Param("name") String name,
                           @Param("sex") String sex,
                           @Param("account") String account,
                           @Param("password") String password,
                           @Param("birthday") String birthday,
                           @Param("mobile") String mobile,
                           @Param("email") String email,
                           @Param("mylevel") String mylevel,
                           @Param("createTime") String createTime,
                           @Param("state") String state,
                           @Param("userType") String userType,
                           @Param("departmentId") String departmentId){
        try {
            User userInfo = userService.findByAccount(account);
            userInfo.setAccount(account);
            userInfo.setBirthday(birthday);
            userInfo.setCreateTime(createTime);
            // if (departmentId!=userInfo.getDepartmentId()) {
            //     departmentDao.addUserTotal(departmentId);
            // }
            userInfo.setDepartmentId(departmentId);
            userInfo.setName(name);
            userInfo.setSex(sex);
            userInfo.setEmail(email);
            userInfo.setMobile(mobile);
            userInfo.setMylevel(mylevel);
            userInfo.setPassword(password);
            userInfo.setState(state);
            userInfo.setUserType(userType);
            userService.editUser(userInfo, departmentId);
            return Return.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

    @RequestMapping("/getDepartmentId")
    public Return getDepartmentId() {
        try {
            return Return.success(userService.getDepartmentId());
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

    @RequestMapping("/addUser")
    @Operation
    public Return addUser(@Param("name") String name,
                          @Param("sex") String sex,
                          @Param("account") String account,
                          @Param("password") String password,
                          @Param("birthday") String birthday,
                          @Param("mobile") String mobile,
                          @Param("email") String email,
                          @Param("mylevel") String mylevel,
                          @Param("createTime") String createTime,
                          @Param("state") String state,
                          @Param("userType") String userType,
                          @Param("departmentId") String departmentId
    ) throws IllegalAccessException {
        User userInfo = new User();
        userInfo.setAccount(account);
        userInfo.setBirthday(birthday);
        userInfo.setCreateTime(TimeConvert.dateToString(new Date()));
        userInfo.setDepartmentId(departmentId);
        userInfo.setName(name);
        userInfo.setSex(sex);
        userInfo.setEmail(email);
        userInfo.setMobile(mobile);
        userInfo.setMylevel(mylevel);
        userInfo.setPassword(password);
        userInfo.setState("0");
        userInfo.setUserType(userType);
        try {
            userService.saveUser(userInfo);
            return Return.success("新增用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

    @RequestMapping("/findAll")
    public Return findAll() {
        try {
            return Return.success(userService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return Return.failure();
        }
    }

}
